<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class media_monitor_manage extends CI_Controller {
	public function __construct() {
		session_start ();
		parent::__construct ();
		$this->load->helper ( "common" );
	}
	public function index() {
		$this->monitor_main ();
	}
	public function monitor_main() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		$data ['update_monitor'] = false;
		if ($this->right_manage->check_right ( "update_monitor" )) {
			$data ['update_monitor'] = true;
		}
		$data ['delete_monitor'] = false;
		if ($this->right_manage->check_right ( "delete_monitor" )) {
			$data ['delete_monitor'] = true;
		}
		$data ['add_monitor'] = false;
		if ($this->right_manage->check_right ( "add_monitor" )) {
			$data ['add_monitor'] = true;
		}
		$data ['query_monitor'] = false;
		if ($this->right_manage->check_right ( "query_monitor" )) {
			$data ['query_monitor'] = true;
		}
		$data ['update_monitor_time'] = false;
		if ($this->right_manage->check_right ( "update_monitor_time" )) {
			$data ['update_monitor_time'] = true;
		}
		$search ['province'] = $this->input->get ( "province" );
		$search ['city'] = $this->input->get ( "city" );
		$search ['region'] = $this->input->get ( "region" );
		$search ['industry'] = $this->input->get ( "industry" );
		$search ['industry_category'] = $this->input->get ( "industry_category" );
		$search ['product_name'] = $this->input->get ( "product_name" );
		$search ['monitor_time'] = $this->input->get ( "monitor_time" );
		$search ['media_type'] = $this->input->get ( "media_type" );
		$search ['media_type_id'] = $this->input->get ( "media_type_id" );
		$search ['brand'] = $this->input->get ( "brand" );
		$search ['dianwei'] = $this->input->get ( "dianwei" );
		$search ['brand_name'] = "";
		
		$this->load->library ( "media/monitor" );
		$data ['monitor_list'] = $this->monitor->query_monitor_list ( $_SESSION ['user_id'], $search );
		$data ['num'] = $this->monitor->query_monitor_num ( $_SESSION ['user_id'], $search );
		
		$this->load->library ( "sys/media_type" );
		$data ['media_type_list'] = $this->media_type->get_all_media_type ();
		
		$this->load->library ( "sys/region" );
		$data ['province_list'] = $this->region->query_user_top_regions ( $_SESSION ['user_id'] );
		$data ['city_list'] = null;
		$data ['region_list'] = null;
		if ($search ['province']) {
			$data ['city_list'] = $this->region->get_regions ( $search ['province'] );
		}
		if ($search ['city']) {
			$data ['region_list'] = $this->region->get_regions ( $search ['city'] );
		}
		$this->load->library ( "sys/industry" );
		$data ['industry_list'] = $this->industry->get_industrys ( 0 );
		if ($search ['industry']) {
			$data ['industry_category_list'] = $this->industry->get_industrys ( $search ['industry'] );
			if ($data ['industry_category_list'] == null) {
				$data ['industry_category_list'] = array (
						array (
								"industry_id" => $search ['industry'],
								"industry_name" => "-" 
						) 
				);
				;
			}
		}
		
		if ($search ['brand']) {
			$this->load->library ( "sys/brand" );
			$brand_info = $this->brand->query_brand_info ( $search ['brand'] );
			$search ['brand_name'] = $brand_info ['brand_name'];
		}
		$data ['search'] = $search;
		$search_url = format_search_url ( $search );
		
		$this->load->library ( "tools/pagination" );
		$data ['page_link'] = $this->pagination->get_pagination ( base_url () . "index.php/media_monitor_manage/monitor_main?", $data ['num'] );
		
		$this->load->view ( "media/monitor/monitor_main", $data );
	}
	public function add_monitor() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "add_monitor", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		
		$this->load->library ( "sys/industry" );
		$data ['industry_list'] = $this->industry->get_industrys ( 0 );
		$this->load->view ( "media/monitor/add_monitor", $data );
	}
	public function save_monitor() {
		$this->load->library ( "common/login_manage" );
		if ($this->login_manage->check_login () == false) {
			$data ['result'] = false;
			$data ['message'] = '请您先登录';
			echo json_encode ( $data );
			die ();
		}
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "add_monitor", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			echo json_encode ( $data );
			die ();
		}
		$monitor ['media_id'] = $this->input->post ( "media_id" );
		$monitor ['industry'] = $this->input->post ( "industry" );
		$monitor ['industry_category'] = $this->input->post ( "industry_category" );
		$monitor ['product_name'] = $this->input->post ( "product_name" );
		$monitor ['brand'] = $this->input->post ( "brand" );
		
		$monitor ['monitor_by'] = $_SESSION ['user_name'];
		$monitor ['monitor_time'] = date ( "Y-m-d H:i:s" );
		$this->load->library ( "media/monitor" );
		$result = $this->monitor->save_monitor ( $monitor );
		$this->load->helper ( "common" );
		echo JSON ( $result );
	}
	public function query_monitor() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "query_monitor", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		
		$monitor_id = $this->input->get ( "monitor_id" );
		
		$this->load->library ( "media/monitor" );
		$data ['monitor_detail'] = $this->monitor->query_monitor_detail ( $monitor_id );
		
		$this->load->view ( "media/monitor/query_monitor", $data );
	}
	public function edit_monitor() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "update_monitor", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		
		$monitor_id = $this->input->get ( "monitor_id" );
		
		$this->load->library ( "media/monitor" );
		$data ['monitor_info'] = $this->monitor->query_monitor ( $monitor_id );
		
		$this->load->library ( "sys/industry" );
		$data ['industry_list'] = $this->industry->get_industrys ( 0 );
		if ($data ['monitor_info'] ['industry'] == $data ['monitor_info'] ['industry_category']) {
			$data ['industry_category_list'] = array (
					array (
							"industry_id" => $data ['industry_category'],
							"industry_name" => "-" 
					) 
			);
		} else {
			$data ['industry_category_list'] = $this->industry->get_industrys ( $data ['monitor_info'] ['industry'] );
		}
		
		$this->load->view ( "media/monitor/edit_monitor", $data );
	}
	public function edit_monitor_time() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/user_menu" );
		$data ['user_menu'] = $this->user_menu->get_user_menu ();
		
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "update_monitor_time", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		
		$monitor_id = $this->input->get ( "monitor_id" );
		
		$this->load->library ( "media/monitor" );
		$data ['monitor_detail'] = $this->monitor->query_monitor_detail ( $monitor_id );
		
		$this->load->view ( "media/monitor/edit_monitor_time", $data );
	}
	
	public function update_monitor_time(){

		$this->load->library ( "common/login_manage" );
		if ($this->login_manage->check_login () == false) {
			$data ['result'] = false;
			$data ['message'] = '请您先登录';
			echo json_encode ( $data );
			die ();
		}
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "update_monitor_time", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			echo json_encode ( $data );
			die ();
		}
		
		$monitor_id = $this->input->post ( "monitor_id" );
		$monitor ['monitor_time'] = $this->input->post ( "monitor_time" );
		$this->load->library ( "media/monitor" );
		$result = $this->monitor->update_monitor_time ( $monitor, $monitor_id );
		$this->load->helper ( "common" );
		echo JSON ( $result );
		
	}
	public function update_monitor() {
		$this->load->library ( "common/login_manage" );
		if ($this->login_manage->check_login () == false) {
			$data ['result'] = false;
			$data ['message'] = '请您先登录';
			echo json_encode ( $data );
			die ();
		}
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "update_monitor", $_SESSION ['user_id'] )) {
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			echo json_encode ( $data );
			die ();
		}
		
		$monitor_id = $this->input->post ( "monitor_id" );
		$monitor ['media_id'] = $this->input->post ( "media_id" );
		$monitor ['industry'] = $this->input->post ( "industry" );
		$monitor ['industry_category'] = $this->input->post ( "industry_category" );
		$monitor ['product_name'] = $this->input->post ( "product_name" );
		$monitor ['monitor_pic'] = $this->input->post ( "monitor_pic" );
		$monitor ['brand'] = $this->input->post ( "brand" );
		
		$monitor ['monitor_by'] = $_SESSION ['user_name'];
		$monitor ['monitor_time'] = date ( "Y-m-d H:i:s" );
		$this->load->library ( "media/monitor" );
		$result = $this->monitor->update_monitor ( $monitor, $monitor_id );
		$this->load->helper ( "common" );
		echo JSON ( $result );
	}
	public function delete_monitor() {
		$this->load->library ( "common/login_manage" );
		$this->login_manage->redirect_url_check_login ();
		
		$this->load->library ( "common/right_manage" );
		if (! $this->right_manage->check_right ( "delete_monitor", $_SESSION ['user_id'] )) {
			$this->load->library ( "common/user_menu" );
			$data ['user_menu'] = $this->user_menu->get_user_menu ();
			$data ['title'] = "权限不足";
			$data ['message'] = "你没有权限使用这个功能。";
			$this->load->view ( "common/error_tip", $data );
			return;
		}
		$monitor_id = $this->input->get ( "monitor_id" );
		$this->load->library ( "media/monitor" );
		$this->monitor->delete_monitor ( $monitor_id );
		echo "<script>history.go(-1)</script>";
	}
}